package com.xiaoge.testproxy;

import com.xiaoge.service.UserService;
import com.xiaoge.service.UserServiceImpl;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.framework.ProxyFactory;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/**
 * TODO ProxyFactory 代理对象工厂, 封装了JDK动态代理和CGLIB
 *
 * @author <a href="mailto:1330137071@qq.com">Zhang Xiao</a>
 * @since
 */
public class TestProxyFactory {

	public static void main(String[] args) {
		UserService target = new UserServiceImpl();

		ProxyFactory proxyFactory = new ProxyFactory();
		proxyFactory.setTarget(target);
		proxyFactory.addAdvice(new MethodInterceptor() {
			@Nullable
			@Override
			public Object invoke(@Nonnull MethodInvocation invocation) throws Throwable {
				System.out.println(".....before.....");
				Object result = invocation.proceed();  // target调用的方法, 例如target.test()
				System.out.println(".....after.....");
				return result;
			}
		});

		UserService userService = (UserService) proxyFactory.getProxy();
//		System.out.println(userService);
		userService.test();
	}

}
